package com.ocom.human.controller;

import com.github.pagehelper.PageInfo;
import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.entity.human.EntityVisitorDetailRecode;
import com.ocom.common.entity.human.EntityVisitorRecord;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.human.*;
import com.ocom.human.service.VisitorDetailRecodeService;
import com.ocom.human.service.VisitorRecordService;
//import com.ocom.human.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class VisitorRecordController {
    private final Logger log = LoggerFactory.getLogger(VisitorRecordController.class);


    @Autowired
    private VisitorRecordService visitorRecordService;


    @Autowired
    private VisitorDetailRecodeService visitorDetailRecodeService;
    /**
     * 获取审批记录列表  (getVisitorRecord)
     * */
    @PostMapping(HumanServiceUrl.URL_GetVisitorRecord)
    public Result GetVisitorRecord(@Validated @RequestBody GetVisitorRecordVo getVisitorRecordVo){
        log.info("获取审批记录列表请求参数{{}}",getVisitorRecordVo);
        Map<String, Object> map = new HashMap<>();
        try {
            List<EntityVisitorRecord> entityVisitorRecords = visitorRecordService.GetVisitorRecord(getVisitorRecordVo.getComId(), getVisitorRecordVo);
            PageInfo<EntityVisitorRecord> entityVisitorRecordPageInfo = new PageInfo<>(entityVisitorRecords);
            map.put("list",entityVisitorRecords);
            map.put("total",entityVisitorRecordPageInfo.getTotal());
        }catch (Exception exception){
            log.error("获取审批记录列表失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"获取审批记录列表失败");
        }
        return ResultUtil.success(map);
    }

    /**
     * 邀请人发起访客申请单  (addVisitorRecord)
     * */
    @PostMapping(HumanServiceUrl.URL_AddVisitorRecord)
    public Result AddVisitorRecord(@Validated @RequestBody AddVisitorRecordVo addVisitorRecord){
        log.info("添加访客申请记录请求参数{{}}",addVisitorRecord);
        Map<String, Object> map = new HashMap<>();
        try {
           EntityVisitorRecord entityVisitorRecord = visitorRecordService.AddVisitorRecord(addVisitorRecord.getComId(), addVisitorRecord);
            map.put("id",entityVisitorRecord.getId());
        }catch (Exception exception){
            log.error("添加访客申请记录失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"添加访客申请记录失败");
        }
        for (GuessVo guessVo: addVisitorRecord.getGuess()) {
            guessVo.setRecodeId(Long.valueOf(map.get("id").toString()));
            log.info("guess{{}}",guessVo);
            visitorDetailRecodeService.AddVisitorDetailRecord(addVisitorRecord.getComId(),guessVo);
        }

        return ResultUtil.success(map);
    }

    /**
     * 补充访客信息  (addVisitorInfo)
     * */
    @PostMapping(HumanServiceUrl.URL_AddVisitorInfo)
    public Result AddVisitorInfo(@Validated @RequestBody AddGuestVo addGuestVo){
        log.info("补充访客信息请求参数{{}}",addGuestVo);
        try {
            for (GuessVo guessVo: addGuestVo.getGuess()) {
                guessVo.setRecodeId(addGuestVo.getRecodeId());
                visitorDetailRecodeService.AddVisitorDetailRecord(addGuestVo.getComId(), guessVo);
            }
        }catch (Exception exception){
            log.error("补充访客信息失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"补充访客信息失败");
        }
        EntityVisitorRecord entityVisitorRecord = new EntityVisitorRecord();
        entityVisitorRecord.setId(addGuestVo.getRecodeId());
        entityVisitorRecord.setApproverStatus(0L);
        visitorRecordService.UpdateVisitorRecord(addGuestVo.getComId(), entityVisitorRecord);
        return ResultUtil.success();
    }

    /**
     * 审批访客申请记录  (updateVisitorRecord)
     * */
    @PostMapping(HumanServiceUrl.URL_UpdateVisitorRecord)
    public Result UpdateVisitorRecord(@Validated @RequestBody UpdateVisitorRecordVo updateVisitorRecordVo){
        log.info("审批访客申请记录请求参数{{}}",updateVisitorRecordVo);
        try {
            visitorDetailRecodeService.UpdateVisitorRecord(updateVisitorRecordVo.getComId(),updateVisitorRecordVo);
        }catch (Exception exception){
            log.error("审批访客申请记录失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"审批访客申请记录失败");
        }
        try {
            //判断是否有未审批的信息
            Integer selectCount = visitorDetailRecodeService.selectCount(updateVisitorRecordVo.getComId(), updateVisitorRecordVo.getRecodeId());
            if (selectCount>0){
                EntityVisitorRecord entityVisitorRecord = new EntityVisitorRecord();
                entityVisitorRecord.setId(updateVisitorRecordVo.getRecodeId());
                entityVisitorRecord.setApproverStatus(5L);
                entityVisitorRecord.setApproverTime(new Date());
                visitorRecordService.UpdateVisitorRecord(updateVisitorRecordVo.getComId(), entityVisitorRecord);
            }
        }catch (Exception exception){
            log.error("修改未审批的信息失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"修改未审批的信息失败");
        }
        return ResultUtil.success();
    }

    /**
     * 更新访客申请记录防疫信息  (updateVisitorRecordByYkm)
     * */
    @PostMapping(HumanServiceUrl.URL_UpdateVisitorRecordByYkm)
    public Result UpdateVisitorRecordByYkm(@Validated @RequestBody EntityVisitorRecord entityVisitorRecord){
        log.info("更新访客申请记录防疫信息请求参数{{}}",entityVisitorRecord);
        try {
           entityVisitorRecord = visitorRecordService.UpdateVisitorRecord(entityVisitorRecord.getComId(), entityVisitorRecord);
        }catch (Exception exception){
            log.error("更新访客申请记录防疫信息失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"更新访客申请记录防疫信息失败");
        }
        return ResultUtil.success(entityVisitorRecord);
    }

    /**
     * 删除访客申请记录（deleteVisitorRecord）
     * */
    @PostMapping(HumanServiceUrl.URL_DeleteVisitorRecord)
    public Result DeleteVisitorRecord(@RequestBody Map<String,Object> map){
        log.info("删除访客申请记录请求参数{{}}",map);
        Long comId = Long.valueOf(map.get("comId").toString()) ;
        Long id = Long.valueOf(map.get("recodeId").toString()) ;
        try {
            visitorRecordService.DeleteVisitorRecord(comId,id);
        }catch (Exception exception){
            log.error("删除访客申请记录失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"删除访客申请记录失败");
        }
        visitorDetailRecodeService.DeleteVisitorDetailRecode(comId,id);
        return ResultUtil.success();
    }

    /**
     * 访客详情页面 (detailVisitorInfo)
     * */
    @GetMapping(HumanServiceUrl.URL_DetailVisitorInfo)
    public Result DetailVisitorInfo(Long comId,Long recodeId){
        log.info("访问详情页面请求参数{{}}，{{}}",comId,recodeId);
        EntityVisitorRecord entityVisitorRecord = visitorRecordService.selectOne(comId, recodeId);
        try {
            List<EntityVisitorDetailRecode> entityVisitorDetailRecodes = visitorDetailRecodeService.DetailVisitorInfo(comId, recodeId);
            log.info("entityVisitorDetailRecodes{{}}",entityVisitorDetailRecodes);
            List<Object> guess = entityVisitorRecord.getGuess();
            guess.addAll(entityVisitorDetailRecodes);
            entityVisitorRecord.setGuess(guess);
            log.info("entityVisitorRecord{{}}",entityVisitorRecord);
        }catch (Exception exception){
            log.error("访问详情页面请求失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"访问详情页面请求失败");
        }
        return ResultUtil.success(entityVisitorRecord);
    }

}
